﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Windows.Forms;
namespace TuoPu镭雕机注册SN并过站
{
    public partial class Form1 : Form
    {
        ArrayList array = new ArrayList();
        public Form1()
        {
            InitializeComponent();
            dataGridviewUI ui = new dataGridviewUI();
            ui.UI(DV_1);
            this.skinEngine1.SkinFile = Application.StartupPath + @"\PageColor2.ssk";
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {

                DV_1.Rows.Clear();
                array.Clear();
                string Serial, NewSerial;
                if (Convert.ToInt32(serialNumberArray.Text) <= 0)
                    return;
                for (int i = 0; i < Convert.ToInt32(serialNumberArray.Text); i++)
                {
                    DV_1.Rows.Add();
                    DV_1.Rows[i].Cells[0].Value = serialNumberRef.Text.Trim();
                    Serial = (Convert.ToInt32(serialNumberRef.Text.Substring(serialNumberRef.Text.Length - 4, 4)) + i).ToString();
                    NewSerial = ("0000" + Serial).Substring(Serial.Length);
                    DV_1.Rows[i].Cells[1].Value = serialNumberRef.Text.Substring(0, serialNumberRef.Text.Length - 4) + NewSerial;
                    DV_1.Rows[i].Cells[2].Value = (i + 1).ToString();
                    array.Add(DV_1.Rows[i].Cells[1].Value.ToString());
                }
            }

            catch (Exception ex)

            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (DV_1.Rows.Count == 0)
            {
                MessageBox.Show(@"请先加载要注册的小板码");
                return;
            }

            try
            {
                string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>");
                string stationNumberTxt = stationNumber.Text.Trim();
                string workOrderNumberTxt = workOrderNumber.Text.Trim();
                string serialNumberRefTxt = serialNumberRef.Text.Trim();
                string serialNumberRefPosTxt = serialNumberRefPos.Text.Trim();
                string processLayerTxt = processLayer.Text.Trim();

                log.AppendText($"{timestamp}trAssignSerialNumberForProductOrWorkOrder\r\n");

                string apiResult = PostAPI.trAssignSerialNumberForProductOrWorkOrder_TUOPU(
                    stationNumberTxt, workOrderNumberTxt, "-1", "-1",
                    serialNumberRefTxt, serialNumberRefPosTxt, processLayerTxt, "0", array);

                JObject resultJ = JObject.Parse(apiResult);
                string returnValue = resultJ["result"]?["return_value"]?.ToString() ?? "无返回值";

                log.AppendText($"{timestamp}{returnValue}\r\n");
                log.AppendText($"{timestamp}trUploadState\r\n");

                string uploadState = PostAPI.trUploadState(stationNumberTxt, processLayerTxt, serialNumberRefTxt);
                log.AppendText($"{timestamp}{uploadState}\r\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作失败: " + ex.Message);
            }
        }

    }
}
